#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cmath>
#include <cstring>

using namespace std;

class Solution
{
public:
    int sumOfGoodSubsequences(vector<int> &nums)
    {
        unordered_map<int, pair<long long, int>> map;
        long long ans = 0;
        int MOD = 1e9 + 7;
        auto getNode = [&](int x)
        {
            if (!map.count(x))
            {
                return make_pair(0LL, 0);
            }
            return map[x];
        };
        for (auto &num : nums)
        {
            long long tsum = 0;

            int l = num - 1, r = num + 1;

            auto [sl, lt] = getNode(l);

            tsum = (tsum + sl + ((long long)lt * num) % MOD) % MOD;

            auto [sr, rt] = getNode(r);

            tsum = (tsum + sr + ((long long)rt * num) % MOD) % MOD;

            tsum = (num + tsum) % MOD;

            long long tt = lt + rt + 1;

            ans = (ans + tsum) % MOD;

            auto [so, ot] = getNode(num);

            map[num] = make_pair((so + tsum) % MOD, (ot + tt) % MOD);
        }
        return ans;
    }
};